package org.csu.wuliu.controller;

import org.csu.wuliu.domain.Category;
import org.csu.wuliu.domain.Item;
import org.csu.wuliu.domain.Product;
import org.csu.wuliu.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/catalog/")
public class CatalogController {


    @Autowired
    private CatalogService catalogService;


    @ResponseBody
    @GetMapping("category")
    public List<Category> getcateList()  {
        System.out.println("来了");

        List<Category> categorys = catalogService.getCategoryList();
        return categorys;
    }

    @ResponseBody
    @DeleteMapping("category")
    public String deletecate(@RequestParam String categoryId)  {
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!"+categoryId);
        catalogService.deleteCategory(categoryId);
        return "成功";
    }


    @PostMapping("category")
    public String newcate( Category category)  {
        System.out.println("0000000000000000000000000000000000000000"+category.getCategoryId());
        catalogService.insertCategory(category);
        return "成功";
    }

    @PatchMapping("category")
    public String updatecate(Category category){
        System.out.println(category.getCategoryId());
        System.out.println(category.getName());
        catalogService.updateCategory(category);

        return "成功";
    }

    @ResponseBody
    @GetMapping("product")
    public List<Product> getprodList(@RequestParam String categoryId)  {

        List<Product> products = catalogService.getProductListByCategory(categoryId);
        return products;
    }

    @ResponseBody
    @DeleteMapping("product")
    public String deleteprod(@RequestParam String productId)  {
        System.out.println("dddddddddd"+productId);
        catalogService.deleteProduct(productId);
        return "成功";
    }


    @PostMapping("product")
    public String newprod( Product product)  {
        System.out.println("0000000000000000000000000000000000000000"+product.getProductId());
        catalogService.insertProduct(product);
        return "成功";
    }

    @PatchMapping("product")
    public String updateprod( Product product){
        System.out.println(product.getProductId());
        System.out.println(product.getName());
        catalogService.updateProduct(product);

        return "成功";
    }
//返回样式
//    [
//    {
//        "itemId": "1",
//            "name": "item1",
//            "warehouseId": "仓库1",
//            "stock": 1000,
//            "sapce": 0,
//            "supplierId": "1",
//            "productId": "1",
//            "username": "wunanxi",
//            "categoryId": "1",
//            "purchase": "10.0",
//            "listprice": "50.0"
//    }
//]

    @ResponseBody
    @GetMapping("item")
    public List<Item> getitemList(@RequestParam String productId)  {
        System.out.println("888888888888888"+productId);
        List<Item> items = catalogService.getItemListByProduct(productId);
        return items;
    }

    @ResponseBody
    @DeleteMapping("item")
    public String deleteitem(@RequestParam String itemId)  {
        catalogService.deleteItem(itemId);
        return "成功";
    }


    @PostMapping("item")
    public String newitem( Item item)  {
        catalogService.insertItem(item);
        return "成功";
    }

    @PatchMapping("item")
    public String updateitem(Item item){
        catalogService.updateItem(item);

        return "成功";
    }




    //wnx增加
    @GetMapping("allItems")
    public List<Item> getAllItem(){
        return catalogService.getAllItems();
    }

}

